package com.ydd.conference.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ydd.conference.R;
import com.ydd.conference.config.AppApplication;
import com.ydd.conference.config.Command;
import com.ydd.conference.config.Constant;
import com.ydd.conference.entity.Message;
import com.ydd.conference.entity.VoteRequest;
import com.ydd.conference.event.Event;
import com.ydd.conference.event.VoteSuccessEvent;
import com.ydd.conference.netty.ConferenceClientHandler;
import com.ydd.conference.util.SharedPreferencesUtil;
import com.ydd.conference.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzpdream on 2017/6/19.
 */

public class SatisfactionActivity extends BaseActivity implements View.OnClickListener{

    private TextView textView;

    private TextView satisfationBtn,almostSatisfationBtn,noSatisfationBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_satisfation);
        initView();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initView();
    }

    private void initView(){
        textView = (TextView) findViewById(R.id.textView);

        satisfationBtn = (TextView) findViewById(R.id.btn_satisfation);
        almostSatisfationBtn = (TextView) findViewById(R.id.btn_almost_satisfation);
        noSatisfationBtn = (TextView) findViewById(R.id.btn_no_satisfation);
        satisfationBtn.setOnClickListener(this);
        almostSatisfationBtn.setOnClickListener(this);
        noSatisfationBtn.setOnClickListener(this);

        textView.setText(Constant.textTitle);
        if (Constant.textTitle == null)
            return;
        if (Constant.textTitle.length() > 15) {
            textView.setTextSize(getResources().getDimension(R.dimen.text_size_10));
            textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        } else {
            textView.setTextSize(getResources().getDimension(R.dimen.text_size_11));
            textView.setGravity(Gravity.CENTER);
        }
    }

    private void initData(){

    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SatisfactionActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn_satisfation:
                sendResult(Constant.SATISFATION);
                break;
            case R.id.btn_almost_satisfation:
                sendResult(Constant.ALMOST_SATISFATION);
                break;
            case R.id.btn_no_satisfation:
                sendResult(Constant.NO_SATISFATION);
                break;
        }

    }

    private void sendResult(final int type){
        AppApplication.executor.submit(new Runnable() {
            @Override
            public void run() {
                VoteRequest.Vote vote = new VoteRequest.Vote();
                vote.value = type;
//                    vote.item = Constant.textTitle;
                vote.id = 1;
                VoteRequest request = new VoteRequest();
                request.setCmd(Command.VOTE.value);
                request.setType(Message.TYPE_MESSAGE_REQUEST);
                VoteRequest.VoteParams params = new VoteRequest.VoteParams();
                params.setSeatId(SharedPreferencesUtil.getSeatId());
                params.setType(Message.TYPE_SINGLE_VOTE);
                List<VoteRequest.Vote> list = new ArrayList<>();
                list.add(vote);
                params.setVotes(list);
                request.setParams(params);
                if (ConferenceClientHandler.mCtx != null) {
                    ConferenceClientHandler.mCtx.writeAndFlush(request);
                }
            }
        });
    }

    /**
     * 接受事件
     *
     * @param event
     */
    public void onEventMainThread(Event event) {
        super.onEventMainThread(event);
        if (event instanceof VoteSuccessEvent) {
            if (SharedPreferencesUtil.getStatus() == Command.VOTE.status &&
                    SharedPreferencesUtil.getVotingRights() == 1 &&
                    SharedPreferencesUtil.getRegisterStatus() == 1) {
                textView.setText("您已评价");
                ViewUtil.showToast("评价成功");
                satisfationBtn.setOnClickListener(null);
                almostSatisfationBtn.setOnClickListener(null);
                noSatisfationBtn.setOnClickListener(null);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (SharedPreferencesUtil.getStatus() == Command.START_VOTE.status || (Constant.isSingleVote && SharedPreferencesUtil.getStatus() == Command.VOTE.status)) {
            if (keyCode != Constant.KEY_AGAINST && keyCode != Constant.KEY_MISS && keyCode != Constant.KEY_AGREE) {
                return false;
            }

            //没有表决权的终端禁用按键
            //TODO by ranfi
            if (SharedPreferencesUtil.getVotingRights() == 0) {
                return false;
            }
            final int finalKeyCode = keyCode;
            AppApplication.executor.submit(new Runnable() {
                @Override
                public void run() {
                    VoteRequest.Vote vote = new VoteRequest.Vote();
                    if (finalKeyCode == Constant.KEY_AGAINST) {
                        vote.value = Constant.NO_SATISFATION;
                    } else if (finalKeyCode == Constant.KEY_AGREE) {
                        vote.value = Constant.SATISFATION;
                    } else if (finalKeyCode == Constant.KEY_MISS) {
                        vote.value = Constant.ALMOST_SATISFATION;
                    }
//                    vote.item = Constant.textTitle;
                    vote.id = 1;
                    VoteRequest request = new VoteRequest();
                    request.setCmd(Command.VOTE.value);
                    request.setType(Message.TYPE_MESSAGE_REQUEST);
                    VoteRequest.VoteParams params = new VoteRequest.VoteParams();
                    params.setSeatId(SharedPreferencesUtil.getSeatId());
                    params.setType(Message.TYPE_SINGLE_VOTE);
                    List<VoteRequest.Vote> list = new ArrayList<>();
                    list.add(vote);
                    params.setVotes(list);
                    request.setParams(params);
                    if (ConferenceClientHandler.mCtx != null) {
                        ConferenceClientHandler.mCtx.writeAndFlush(request);
                    }
                }
            });
        }
        return super.onKeyDown(keyCode, event);
    }
}
